﻿using System;
using UnityEngine;
using System.Collections;

using UnityEngine.UI;
using System.Collections.Generic;
using DG.Tweening;

public class NumPick : MonoBehaviour
{
    public int Index;
    public Transform listtf;
    public List<Text> PTexts = new List<Text>();
    private Vector3 startPos;
    private Vector3 endPos;

    public Text EndText;
    public Text StartText;
    public int EndNum;
    public Action ComplectAction;
    public bool isComMessage;
    public Transform textTf;
    public List<Text>  testTexts = new List<Text>();
    public Animation TesAnimation;
    public string groupTag;
    public List<int> pickNum = new List<int>(); 

	// Use this for initialization
	void Start ()
	{
	    isComMessage = false;
        ComplectAction = null;
        for (int i = 0; i < listtf.childCount; i++)
	    {
	        Transform tf= listtf.GetChild(i);
	        PTexts.Add(tf.FindChild("Text").GetComponent<Text>());

	    }

        for (int i = 0; i < textTf.childCount; i++)
        {
            Transform tf = textTf.GetChild(i);
            testTexts.Add(tf.FindChild("Text").GetComponent<Text>());

        }

        RectTransform rectf = listtf.GetComponent<RectTransform>();
        startPos = listtf.transform.localPosition;
        
        endPos = new Vector3(startPos.x, rectf.sizeDelta.y * -1 - startPos.y +10);
	  //  SetTextNum();
	}


    public void SetTextNum()
    {
        groupTag = GlobalVO.GetInstance().NumAllInfo.SetDataDic["当前标识"];
        StartText.text ="";
        int count = PTexts.Count;
        List<int> ls = new List<int>();
        if (GlobalVO.GetInstance().NumAllInfo==null)
        {
            return;
        }
        
        int dcount = GlobalVO.GetInstance().NumAllInfo.numberCountdic[GlobalVO.GetInstance().NumAllInfo.SetDataDic["批次"]];

        int cks = dcount;
        if (cks<41)
        {
            cks = 45;
        }
        for (int i = 0; i < cks; i++)
        {
            ls.Add(i+1);
        }

        ls = GetRandomList(ls);

        for (int i = 0; i < count; i++)
        {
            Text tx = PTexts[i];
            Text ntx = testTexts[i];
            string lestr = getNumbertoStr( ls[i]);
 

            ntx.text = lestr;

            if (tx== StartText || tx == EndText)
            {
                continue;
            }
            tx.text = lestr;
        }

        EndText.text = getNumbertoStr(EndNum) ;

    }

    public string getNumbertoStr(int num)
    {

        return UtilTool.getNumbertoStr(num);
    }


    public static List<T> GetRandomList<T>(List<T> inputList)
    {
        //Copy to a array
        T[] copyArray = new T[inputList.Count];
        inputList.CopyTo(copyArray);

        //Add range
        List<T> copyList = new List<T>();
        copyList.AddRange(copyArray);

        //Set outputList and random
        List<T> outputList = new List<T>();
        System.Random rd = new System.Random(DateTime.Now.Millisecond);

        while (copyList.Count > 0)
        {
            //Select an index and item
            int rdIndex = rd.Next(0, copyList.Count - 1);
            T remove = copyList[rdIndex];

            //remove it from copyList and add it to output
            copyList.Remove(remove);
            outputList.Add(remove);
        }
        return outputList;
    }


    public void TimeRun()
    {
        listtf.gameObject.SetActive(false);
        TesAnimation.gameObject.SetActive(true);
        TesAnimation.Play();
        // Invoke("OnRun", 0.1f);
    }

    public void Stop()
    {
        TesAnimation.Stop();
        
        TesAnimation.gameObject.SetActive(false);
        listtf.gameObject.SetActive(true);
        OnRun();
    }

    public void OnRun()
    {
        //
        RectTransform rectf = listtf.GetComponent<RectTransform>();
        listtf.localPosition = startPos;
        Tweener tw = listtf.DOLocalMoveY(endPos.y, 1f);
        tw.OnComplete(OnComplete);
    }


    void OnComplete()
    {
        if (isComMessage)
        {

            Messenger<bool>.Broadcast("移动完成", true, MessengerMode.DONT_REQUIRE_LISTENER);
        }

    }

}
